package com.example.demo;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, ViewPager.OnTouchListener {

    private FluentViewPager imagePager;
    private ForegroundView fgView;
    private ImageDetailsAdapter mAdapter;
    private ArrayList<LinearLayout> itemData;
    private int[] images = {R.mipmap.i_01, R.mipmap.i_02, R.mipmap.i_03, R.mipmap.i_04};

    private float x0;
    private float x1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        init();

    }

    private void init() {
        this.fgView = (ForegroundView) findViewById(R.id.fg_view);
        this.imagePager = (FluentViewPager) findViewById(R.id.image_pager);
        this.imagePager.setOffscreenPageLimit(3);//设置缓存条数
        this.imagePager.setPageMargin(-450);//设置相邻pager之间的间隔
        this.imagePager.setPageTransformer(true, new FluentFlingAnim());//设置每个页面的动画
        this.itemData = new ArrayList<>();
        this.mAdapter = new ImageDetailsAdapter(itemData);
        this.imagePager.setAdapter(mAdapter);
        this.imagePager.addOnPageChangeListener(this);
        this.imagePager.setOnTouchListener(this);
        initData();
    }

    private void initData() {
        for (int item : images) {
            /**
             * 》如果直接添加A:imageView，在加入viewPager时，系统会默认会将其放入一个新建的B:ViewGroup对象里，
             * 而B的width与Height都是适应屏幕尺寸的；
             * 》只有当我们传入给viewPager的直接是ViewGroup或其子类，系统才不会在创建ViewGroup对象；er显示才会
             * 像我们设置的那样
             * 》换句话说：如果直接给ImageView，那么我们给imageView设置的LayoutParams将没有效果
             */
            LinearLayout group = new LinearLayout(this);
            ViewGroup.LayoutParams groupParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            group.setLayoutParams(groupParams);
            group.setGravity(Gravity.CENTER);
            ImageView iv = new ImageView(this);
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(400, 320);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(item);
            group.addView(iv);
            itemData.add(group);
        }
        this.mAdapter.notifyDataSetChanged();//don`t forget
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x0 = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                x1 = event.getX();
                break;
        }
        if ((x1 - x0) < 0) {//根据左滑，还是右滑设置相反动画
            fgView.initAnimation(60f);
        } else if ((x1 - x0) > 0F) {
            fgView.initAnimation(-60f);
        }
        return false;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        fgView.setSelectedTxtColor(position);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
